<?php 
Class wxkeyController extends appController
{
	
	public function index()
	{
		
		$page=isset($_REQUEST['page'])?intval($_REQUEST['page']):1;
		$sort['keyword']=isset($_GET['k'])?trim($_GET['k']):'';
		$art_list=Model('wxkey')->key_list($page,$sort);
		
		$this->assign('ur_here','>关键词列表');
		$this->assign('art_list',$art_list['data']);
		$this->assign('art_page',$art_list['pagelink']);
		
		
		$this->display('index.html');
		
	}
	public function ordering()
	{
		$id=isset($_POST['id'])?intval($_POST['id']):'';
		$val=isset($_POST['val'])?intval($_POST['val']):'';
		$result=Model('article')->ordering($val,$id);
		print_r(json_encode($result));
	}
	public function del()
	{
		$id=isset($_REQUEST['id'])?$_REQUEST['id']:'';
		
		if(is_array($id))
		{
			foreach($id as $k=>$v)
			{
				$result=Model('wxkey')->del_key($v);
			}
		}
		else
		{
			$id=intval($id);
			
			$result=Model('wxkey')->del_key($id);
		}
		print_r(json_encode($result));
	
	}
	public function edit_key()
	{
	
		$id=isset($_GET['id'])?intval($_GET['id']):'';
		$this->assign('id',$id);
		
		$menu_list=Model('wxrule')->get_rule_list();
        $this->assign('rule_list',$menu_list);
		$this->assign('ur_here','>编辑关键词');
		$art_info=Model('wxkey')->get_key($id);
		$this->assign('art',$art_info);
		$this->assign('action','modified_key');
		
		$this->display('key.html');
	
	}
	public function modified_key()
	{
		$id=isset($_POST['id'])?$_POST['id']:'';
	     $info['keyword']=isset($_POST['keyword'])?$_POST['keyword']:'';
		$info['rid']=isset($_POST['rule_id'])?intval($_POST['rule_id']):'';
		//$info['status']=isset($_POST['status'])?intval($_POST['status']):'';
		$info['type']=isset($_POST['type'])?intval($_POST['type']):'';
		$data=array('error'=>1,'content'=>'','url'=>'');
		if(Model('wxkey')->check_key($info['keyword'],$id))
		{
		    $result=Model('wxkey')->update_key($info,$id);
		
		
		    if($result)
		   {
			$data['error']=0;
			$data['content']=Lang('SUCCESS_EDIT');
			$data['url']="index.php?g=admin&m=wxkey&a=index";
			
		   }
		   else
		   {
			$data['content']=Lang('INVALID_OPERATION');
			//$data['url']=$_SERVER['REQUEST_URI'];
		    }
		}
		$this->showMsg($data);
	}
     public function add_key()
	{
		$this->assign('ur_here','>添加关键词');
		$menu_list=Model('wxrule')->get_rule_list();
		
		$this->assign('rule_list',$menu_list);
		$this->assign('action','new_key');
		$this->display('key.html');
	
	}
	
	public function new_key()
	{
		
		
		$info['keyword']=isset($_GET['k'])?$_GET['k']:'';
		$info['rid']=isset($_POST['rule_id'])?intval($_POST['rule_id']):'';
		$info['status']=isset($_POST['status'])?intval($_POST['status']):0;
		$info['type']=isset($_POST['type'])?intval($_POST['type']):0;
	
		
		$data=array('error'=>0,'content'=>'','url'=>'');
		if(Model('wxkey')->check_key($info['keyword']))
		{
		   if(Model('wxkey')->create_key($info))
		  {
			$data['content']=Lang('SUCCESS_ADD');
			$data['url']="index.php?g=admin&m=wxkey&a=index";
			
		  }
		   else
		  {
			$data['content']=Lang('INVALID_OPERATION');
			//$data['url']=$_SERVER['REQUEST_URI'];
		  }
		}
		$this->showMsg($data);
	}
	
	public function file()
	{
		
		$result=array('error'=>0,'url'=>$_FILES['thumb']['name']);
		$thumb_url=Model('article')->upload_img($_FILES['thumb']);
		$result['url']=$thumb_url;
		
		$result['message']='';
		print_r(json_encode($result));
		exit();
	}
	
	

	
	
}